package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"macapp/models"
	"strconv"
	"strings"
	"unsafe"
)

type AdminClassificationController struct {
	beego.Controller
}

func (a *AdminClassificationController) Index() {
	a.TplName = "admin/classification.html"
}

func (a *AdminClassificationController) Add() {
	a.Data["id"]=strings.ToUpper(models.GetRandomString(16))
	a.TplName = "admin/classification-add.html"
}

func (a *AdminClassificationController) GetAll() {
	var cf_json models.ClassificationJson
	var cfs []models.Classification

	page := a.GetString("page")
	limit := a.GetString("limit")
	int_page, _ := strconv.Atoi(page)
	int_limit, _ := strconv.Atoi(limit)

	qs := models.O.QueryTable(new(models.Classification))
	qs.Limit(int_limit, (int_page-1)*int_limit).OrderBy("-id").All(&cfs)

	cf_json.Code = 0
	cf_json.Msg = ""
	cf_json.Count = GetCfCounts()
	cf_json.Data = cfs
	a.Data["json"] = &cf_json
	a.ServeJSON()
}

func (a *AdminClassificationController) AddApi() {
	var cfs models.Classification
	cfs.Id=a.GetString("id")
	cfs.Cfname=a.GetString("cfname")

	_,err:=models.O.Insert(&cfs)
	fmt.Println(err)
	a.Ctx.WriteString("success ")
}


func GetCfCounts() int {
	int64_cnt, _ := models.O.QueryTable(new(models.Classification)).Count()
	//int64 转int
	cnt := *(*int)(unsafe.Pointer(&int64_cnt))
	return cnt
}
